package com.yuwei.home.commentdialog

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.yuwei.common.mvi.UiState
import com.yuwei.home.HomeIntent
import com.yuwei.home.HomeViewModel
import com.yuwei.home.R
import com.yuwei.home.adapter.VideoCommentListAdapter
import com.yuwei.home.databinding.DialogCommendListLayoutBinding
import com.yuwei.home.entity.CommentEntity
import com.yuwei.home.entity.CommentEntityItem
import com.yuwei.home.entity.Reply
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

/**
 * 自定义dialog实现评论列表弹窗
 */

class CommentDialog constructor(private val itemId:String):DialogFragment() {
    private lateinit var mDataBinding: DialogCommendListLayoutBinding
    private val mViewModel:HomeViewModel by activityViewModels()
    private val  commentList = mutableListOf<CommentEntityItem>()
    private val replayCommentList = mutableListOf<Reply>()
    private lateinit var commentListAdapter:VideoCommentListAdapter
    private var videoItemId = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置样式
        setStyle(DialogFragment.STYLE_NO_TITLE,R.style.BottomSheetDialog)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.setCanceledOnTouchOutside(true)//设置外部区域可以取消
        dialog.window?.apply {
            setGravity(Gravity.BOTTOM)
            setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT)//布局
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))//透明背景
        }
        return dialog
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
        mDataBinding =  DialogCommendListLayoutBinding.inflate(inflater, container, false)
        return mDataBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initData()
        updateUi()
    }

    private fun updateUi() {
      lifecycleScope.launch{
          repeatOnLifecycle(Lifecycle.State.STARTED){
                mViewModel.state.collectLatest {
                    when(it){
                        is UiState.onSuccess<*> ->{
                            showVideoCommentList(it.data as CommentEntity)
                        }
                        else -> {}
                    }
                }
          }
      }
    }
    private fun showVideoCommentList(list: CommentEntity) {
        commentList.clear()
        commentList.addAll(list)
        commentListAdapter.notifyDataSetChanged()
    }
    private fun initData() {
        lifecycleScope.launch {
            mViewModel.sendIntent(HomeIntent.getComment(0,itemId))
        }
    }

    private fun initView() {
        commentListAdapter = VideoCommentListAdapter(commentList)
        mDataBinding.commendList.expandList.setAdapter(commentListAdapter)
    }
}